--
-- Author: shunguo.chen
-- Date: 2018-07-06 16:12:43
--
-- AvatarPreView 外显预览
--

local AvatarFactory = app:getClass("AvatarFactory")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.AvatarPreviewScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		executeFunc(self._onCloseView, self)
	end)

end

-- params = {
-- 	type = Enums.AvatarPreviewType,
-- 	id = equipId/mountId/partnerId
-- }
function ClassRef:refresh(params, avatarParams)
	local tp = params.type
	local armature
	local actionName = "mapidle"
	if tp == Enums.AvatarPreviewType.equip then
		armature = AvatarFactory.createHero(avatarParams)
	elseif tp == Enums.AvatarPreviewType.mount then
		armature = AvatarFactory.createRideById(params.id, false)
		actionName = "ride_idle"
	elseif tp == Enums.AvatarPreviewType.partner then
		armature = AvatarFactory.createPartnerByConf(params.data:getShowAvatorParams())
		actionName = "mapidle"
	else
		armature = AvatarFactory.createNpcById(params.id)
	end

	if armature then
		self._ui.Node:addChild(armature)
		if armature:hasAnimation(actionName) then
			armature:play(actionName)
		end

		-- 坐骑需要适配一下位置
		if tp == Enums.AvatarPreviewType.mount then
			local conf = GD:getMountResConfig(params.id)
			local size = tonumber(conf.size or 100)/100
			armature:setScale(size)
			local box = armature:getBoundingBox()
			local pos = self._ui.Node:convertToNodeSpace(cc.p(box.x + box.width/2, box.y))
			armature:setPositionX(-pos.x)
		end
	end
end

return ClassRef
